﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using UtilsLib.UtilsFile;
using UtilsLib.UtilsType;

namespace UtilsLib.UtilsLog
{
    public static class LogToFile
    {
        private static string _pathToFile;
        private static StreamWriter _file;
        private static bool _isLogging = false;

        /// <summary>
        /// Indique si le Logger est initialisé
        /// </summary>
        public static bool IsInitialized
        {
            get
            {
                return _isLogging;
            }
        }

        /// <summary>
        /// Init the LogToFile, Create the file, directory,...
        /// </summary>
        /// <param name="filePath">The path of the file (without the name)</param>
        /// <param name="fileName">The name of the file (without the path)</param>
        /// <param name="extension">The extension of the file</param>
        internal static void Initialise(string filePath, string fileName, string extension)
        {
            _pathToFile = FileLib.CreateFile(filePath, fileName, extension, false);
            _isLogging = true;
            WriteLine(DateTime.Now.ToString("dd'/'MM'/'yyyy"), TypeMessage.AppStart);
        }

        /// <summary>
        /// Write a line in the log with an handler
        /// </summary>
        /// <param name="message">The message to write</param>
        /// <param name="type">The type of message (Error, Warning, ...)</param>
        private static void WriteLine(string message, TypeMessage type)
        {
            if (_isLogging)
            {
                _file = new StreamWriter(_pathToFile, true);
                string header;

                header = DateTime.Now.ToString("[HH:mm:ss: fff]") + "[" + type.ToString() + "] ";

                _file.WriteLine(header + message);
                _file.Close();

                LogToConsole.WriteConsoleLine(type, message, true);
            }
        }

        /// <summary>
        /// Write a line in the log
        /// </summary>
        /// <param name="message">The message to write in the log</param>
        public static void WriteMessage(params string[] message)
        {
            foreach (string s in message)
                WriteLine(s, TypeMessage.Message);
        }

        /// <summary>
        /// Write an exception line in the log with various informations
        /// </summary>
        /// <param name="ex">The exception</param>
        /// <param name="message">The message you want to display</param>
        /// <param name="methodeName"> The name of the methode where the error is raised</param>
        public static void WriteException(Exception ex, string message, string methodeName)
        {
            WriteLine("Method : " + methodeName + " | Message : " + message + " | Exception : " + ex.Message, TypeMessage.Error);
        }

        /// <summary>
        /// Write an exception line in the log with various informations
        /// </summary>
        /// <param name="ex">The exception</param>
        /// <param name="message">The message you want to display</param>
        /// <param name="Request"> The name of the methode where the error is raised</param>
        public static void WriteException(Exception ex, string message, StringBuilder strb)
        {
            WriteLine("Requete SQL : " + strb.ToString() + " | Message : " + message + " | Exception : " + ex.Message, TypeMessage.Error);
        }

        /// <summary>
        /// Write an exception line in the log with various informations
        /// </summary>
        /// <param name="ex">The exception</param>
        /// <param name="message">The message you want to display</param>
        public static void WriteException(Exception ex, string message)
        {
            WriteLine("Message : " + message + " | Exception : " + ex.Message, TypeMessage.Error);
        }

        /// <summary>
        /// Write an exception line in the log with various informations
        /// </summary>
        /// <param name="ex">The exception</param>
        public static void WriteException(Exception ex)
        {
            WriteLine(ex.Message, TypeMessage.Error);
        }

        /// <summary>
        /// Write an Error line in the log when something is not supposed to happen but does !
        /// </summary>
        /// <param name="message">The message(s) to describe the error</param>
        public static void WriteError(params string[] message)
        {
            foreach (string s in message)
                WriteLine(s, TypeMessage.Error);
        }

        /// <summary>
        /// Write a warning in the log
        /// </summary>
        /// <param name="message">The message to describe the warning</param>
        public static void WriteWarning(string message)
        {
            WriteLine(message, TypeMessage.Warning);
        }


        /// <summary>
        /// Final message to display
        /// </summary>
        public static void CloseFile()
        {
            WriteLine(DateTime.Now.ToString("dd'/'MM'/'yyyy") + " --------------------------------------", TypeMessage.AppEnd);
        }

        /// <summary>
        /// Final message to display
        /// </summary>
        public static void CloseFile(string message)
        {
            WriteLine(DateTime.Now.ToString("dd'/'MM'/'yyyy") + " " + message, TypeMessage.AppEnd);
        }

    }



}
